﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;

namespace LowLevelGraphics.Filter
{
    /// <summary>
    /// 
    /// </summary>
    public class Experimental4 : BaseImageFilter
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="Experimental4"/> class.
        /// </summary>
        public Experimental4()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Experimental4"/> class.
        /// </summary>
        /// <param name="_experimental4">The _experimental4.</param>
        internal Experimental4(Experimental4 _experimental4)
        {
        }

        /// <summary>
        /// Execute this Filter
        /// A Debug Statement is written if a concrete Filter is called
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            UnsafeBitmap bitmap = _bitmap;

            int nRe = 0;
            int nGe = 0;
            int nBe = 0;

            int nWidth = bitmap.Width;
            int nHeight = bitmap.Height;

            Color color = Color.Transparent;

            int y = 0;
            int x = 0;

            for (y = 0; y < nHeight; y++)
            {
                for (x = 0; x < nWidth; x++)
                {
                    color = bitmap.GetPixel(x, y);
                    nRe += color.R;
                    nGe += color.G;
                    nBe += color.B;
                }
            }

            int nPixelCount = nHeight * nWidth;
            nRe /= nPixelCount;
            nGe /= nPixelCount;
            nBe /= nPixelCount;

            for (y = 0; y < nHeight; y++)
            {
                for (x = 0; x < nWidth; x++)
                {
                    color = bitmap.GetPixel(x, y);
                    float fBG = 0.0f;
                    float fRB = 0.0f;
                    float fRG = 0.0f;
                    ColorFunctions.GetChannelRatios(color, out fBG, out fRB, out fRG);

                    float nR = color.R;
                    float nG = color.G;
                    float nB = color.B;

                    float fDiff = Math.Abs(Math.Abs(fBG - fRG) - Math.Abs(fRG - fRB));

                    if (color.R > nRe && color.G > nGe && color.B > nBe)
                    {
                        nR *= fDiff;
                        nG *= fDiff;
                        nB *= fDiff;
                    }
                    else
                    {
                        //nR *= fRB*fRG;
                        //nG *= fBG*fRG;
                        //nB *= fBG*fRB;
                        nR *= fDiff;
                        nG *= fDiff;
                        nB *= fDiff;
                    }
                    nR %= 255;
                    nG %= 255;
                    nB %= 255;
                    bitmap.SetPixel(x, y, Color.FromArgb((int)nR, (int)nG, (int)nB));
                    /*if (fDiff > 127)
                    {
                        nR = 255;
                        nG = 255;
                        nB = 255;
                    }
                    else if (fDiff < 127)
                    {
                        nR = 0;
                        nG = 0;
                        nB = 0;
                    }
                    else
                    {
                        nR = 255;
                        nG = 0;
                        nB = 0;
                    }*/
                    //bitmap.SetPixel(x, y, Color.FromArgb(nR, nG, nB));
                }
            }
            return _bitmap;
        }

        /// <summary>
        /// This is used to clone a filter, each filter must implement it's own clone functionality
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new Experimental4(this);
        }
    }
}
